
import sun.audio.AudioPlayer;

import java.awt.*;

import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.image.BufferedImage;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class World extends Frame implements Runnable {
    private boolean firstTime = true;
    private BufferedImage bi;//缓冲图片
    private Graphics2D big;
    AlphaComposite alphaComposite2 = AlphaComposite.getInstance(3, 1.0F);
    public float mapPosition = 0.0F;//地图位置
    private FloatPoint bgPosition = new FloatPoint(20.0F, 90.0F);
    private FloatPoint scenePosition = new FloatPoint(0.0F, 0.0F);//场景位置
    private int sceneWidth = 3328;//场景的宽度
    private int backgroundSizeOfWidth = 900;//背景的宽度
    private int backgroundSizeOfHeight = 672;//背景的高度
    Toolkit toolkit = Toolkit.getDefaultToolkit();//工具包
    Dimension screenSize = this.toolkit.getScreenSize();//屏幕尺寸
    private Hero hero;//英雄
    private static int FPS = 60;
    private int walkSpeed = 1;//移动速度
    private GrassLand[] grassLands = new GrassLand[41];//草原数组
    private Random random = new Random();//随机数
    List<Bullet> heroBullets;//英雄子弹表
    List<Bullet> enemyBullets;//敌人子弹表
    private boolean leftDown = false;//左走
    private boolean rightDown = false;//右走
    private boolean upDown = false;//前进
    private boolean downDown = false;//后退
    private boolean jumpDown = false;//跳跃
    private int heroNum = 30;//英雄的命数
    private List<SimplePlayer2> enemys = new ArrayList();//拿枪敌人的动态数组
    private boolean showBossScene = false;//boss场景

    int simpleEnemyNum = 0;
    private Boss boss;//boss
    GameStartBackgroundSoundThread gameStartBgSoundThread;//游戏开始背景声音
    PreBossBackgroundSoundThread preBossBgSoundThread;//boss背景声音线程
    boolean bossBattle = false;//boss战斗
    private boolean passedFirstMovingGrassLand = false;//第一次经过草地的移动
    private boolean passedSecondMovingGrassLand = false;//第二次经过草地的移动


    /**
     * 主世界
     * <p>
     * 这是目前想到的类
     * 大家写的时候缺少类的自己创建
     * 名称尽量统一：方便合并
     *
     * @param
     */
    public World() {
        this.gameStartBgSoundThread = new GameStartBackgroundSoundThread();//游戏开始背景声音
        //  new Thread(this.gameStartBgSoundThread).start();
        this.hero = new Hero(new FloatPoint(50.0F, 30.0F), 2);
        this.heroBullets = new ArrayList();//英雄子弹数组
        this.enemyBullets = new ArrayList();//敌人子弹数组
        initGrassLands();//初始化草地
        initEnemys();//初始化敌人
        setSize(this.backgroundSizeOfWidth, this.backgroundSizeOfHeight);//设置背景的高和宽
        setLocation((this.screenSize.width - getWidth()) / 2, 20);//设置位置屏幕的宽度
        //setVisible(false);

        addKeyListener(new KeyListener()//添加键盘事件监听器
        {
            public void keyTyped(KeyEvent e) {
                if (KeyEvent.getKeyText(e.getKeyChar()).equals("Esc"))//退出键
                    System.exit(0);
            }

            public void keyReleased(KeyEvent e) {//释放键盘
                World.this.setKeyStateWhenKeyReleased(e);
                if ((World.this.hero.state != 4) && (World.this.hero.state != 5)) {
                    if (KeyEvent.getKeyText(e.getKeyCode()).equals("J")) {//英雄射击键
                        World.this.hero.canShot = true;
                    }
                    if (KeyEvent.getKeyText(e.getKeyCode()).equals("K"));
                }
            }

            public void keyPressed(KeyEvent e) {//按下键盘
                int type = e.getKeyCode() - 48;
                if ((type > 0) && (type < 5)) {
                    World.this.hero.weapon = WeaponFactory.getWeapon(type);//英雄获取武器
                }

                World.this.setKeyStateWhenKeyPressed(e);
                if ((World.this.hero.state != 4) && (World.this.hero.state != 5)) {//英雄不能死亡掉落状态发射子弹
                    if (KeyEvent.getKeyText(e.getKeyCode()).equals("J")) {//射击键的情况
                        if (World.this.hero.canShot) {
                            if (World.this.hero.weapon instanceof NunLinerWeapon)
                            {
                                World.this.heroBullets.addAll(World.this.hero.shot(1));
                            } else if (World.this.hero.weapon instanceof SimpleWeapon)
                            {
                                World.this.heroBullets.addAll(World.this.hero.shot(1));
                            } else if (World.this.hero.weapon instanceof StrongWeapon)
                            {
                                World.this.heroBullets.addAll(World.this.hero.shot(1));
                            } else if (World.this.hero.weapon instanceof ShotWeapon)
                            {
                                World.this.heroBullets.addAll(World.this.hero.shot(10));
                            }
                        }
                        World.this.hero.canShot = false;
                    }

                    if ((KeyEvent.getKeyText(e.getKeyCode()).equals("K")) &&//跳跃的的状态
                            (World.this.hero.canDrop) && (World.this.hero.state == 3) &&
                            (World.this.hero.position.y < World.this.backgroundSizeOfHeight - 60)) {
                        World.this.hero.canDrop = false;
                    }
                }
            }
        });
    }

    private void setKeyStateWhenKeyPressed(KeyEvent e) {//键位
        if ((this.hero.state != 4) && (this.hero.state != 5)) {
            if (KeyEvent.getKeyText(e.getKeyCode()).equals("W")) {//前进
                this.upDown = true;
            }
            if (KeyEvent.getKeyText(e.getKeyCode()).equals("S")) {//后退
                this.downDown = true;
            }
            if (KeyEvent.getKeyText(e.getKeyCode()).equals("A")) {//左走
                this.leftDown = true;
            }
            if (KeyEvent.getKeyText(e.getKeyCode()).equals("D")) {//右走
                this.rightDown = true;
            }
            if (KeyEvent.getKeyText(e.getKeyCode()).equals("K"))//跳跃
                this.jumpDown = true;
        }
    }

    private void setKeyStateWhenKeyReleased(KeyEvent e) {//键位
        if ((this.hero.state != 4) && (this.hero.state != 5)) {
            if (KeyEvent.getKeyText(e.getKeyCode()).equals("W")) {//前进
                this.upDown = false;
            }
            if (KeyEvent.getKeyText(e.getKeyCode()).equals("S")) {//后退
                this.downDown = false;
            }
            if (KeyEvent.getKeyText(e.getKeyCode()).equals("A")) {//左走
                this.leftDown = false;
            }
            if (KeyEvent.getKeyText(e.getKeyCode()).equals("D")) {//右走
                this.rightDown = false;
            }
            if (KeyEvent.getKeyText(e.getKeyCode()).equals("K"))//跳跃
                this.jumpDown = false;
        }
    }

    private void initEnemys() {

    }

    private void initGrassLands() {
        FloatPoint[] points = {new FloatPoint(1.0F, 110.0F), new FloatPoint(5.0F, 142.0F), new FloatPoint(8.0F, 174.0F),
                new FloatPoint(9.0F, 206.0F), new FloatPoint(11.0F, 174.0F), new FloatPoint(13.0F, 142.0F), new FloatPoint(18.0F, 206.0F),
                new FloatPoint(19.0F, 158.0F), new FloatPoint(27.0F, 110.0F), new FloatPoint(36.0F, 110.0F), new FloatPoint(42.0F, 78.0F),
                new FloatPoint(43.0F, 206.0F), new FloatPoint(46.0F, 160.0F), new FloatPoint(49.0F, 142.0F), new FloatPoint(53.0F, 206.0F),
                new FloatPoint(57.0F, 110.0F), new FloatPoint(59.0F, 174.0F), new FloatPoint(62.0F, 174.0F), new FloatPoint(63.0F, 78.0F),
                new FloatPoint(65.0F, 158.0F), new FloatPoint(67.0F, 142.0F), new FloatPoint(69.0F, 110.0F), new FloatPoint(72.0F, 142.0F),
                new FloatPoint(72.0F, 206.0F), new FloatPoint(73.0F, 174.0F), new FloatPoint(76.0F, 110.0F), new FloatPoint(77.0F, 78.0F),
                new FloatPoint(77.0F, 206.0F), new FloatPoint(78.0F, 158.0F), new FloatPoint(80.0F, 110.0F), new FloatPoint(81.0F, 142.0F),
                new FloatPoint(84.0F, 206.0F), new FloatPoint(88.0F, 174.0F), new FloatPoint(91.0F, 142.0F), new FloatPoint(93.0F, 110.0F),
                new FloatPoint(93.0F, 206.0F), new FloatPoint(94.0F, 158.0F), new FloatPoint(98.0F, 142.0F), new FloatPoint(99.0F, 174.0F),
                new FloatPoint(24.0F, 110.0F), new FloatPoint(33.0F, 110.0F)};
        int[] lengths = {22, 3, 1, 2, 1, 2, 2, 3, 5, 8, 16, 3, 2, 7, 6, 7, 2, 2, 5, 1, 3, 2, 2, 1, 3, 2,
                2, 1, 1, 2, 5, 3, 2, 2, 5, 22, 4, 1, 1, 1, 1};
        for (int i = 0; i < points.length - 2; i++) {
            GrassLand gl = new GrassLand(points[i], lengths[i]);
            this.grassLands[i] = gl;
        }
        GrassLand movingGrassLand1 = new GrassLand(new FloatPoint(24.0F, 110.0F), 1);
        GrassLand movingGrassLand2 = new GrassLand(new FloatPoint(33.0F, 110.0F), 1);
        movingGrassLand1.movingDirection = 3;
        movingGrassLand2.movingDirection = 3;
        this.grassLands[(this.grassLands.length - 2)] = movingGrassLand1;
        this.grassLands[(this.grassLands.length - 1)] = movingGrassLand2;
    }

    private void drawNPC(Graphics2D g) {
        this.grassLands[(this.grassLands.length - 2)].drawMovingGrassLand(this.hero, 23.4F, 25.799999F, (int) this.mapPosition, this.backgroundSizeOfWidth, this, g);
        this.grassLands[(this.grassLands.length - 1)].drawMovingGrassLand(this.hero, 32.400002F, 34.799999F, (int) this.mapPosition, this.backgroundSizeOfWidth, this, g);
    }

    private void playPreBossBattleAnimation(Graphics2D g) {
    }

    private void playBossBattleAnimation(Graphics2D g) {

    }
//画英雄子弹
    private void drawHeroBullets(Graphics2D g)
    {

        for (int i = 0; i < this.heroBullets.size(); i++)
        {
            int x = (int)((Bullet)this.heroBullets.get(i)).position.x;
            int y = (int)((Bullet)this.heroBullets.get(i)).position.y;
            boolean isAlive = ((Bullet)this.heroBullets.get(i)).isAlive;
            if ((x < this.mapPosition) || (x > this.backgroundSizeOfWidth / 3.0F + this.mapPosition) || (y < 0) || (y > this.backgroundSizeOfHeight / 3.0F) || (!isAlive)) {
                this.heroBullets.remove(i);
            }
            else
                ((Bullet)this.heroBullets.get(i)).drawBubblet(g, this.mapPosition);
        }
    }

    private void drawSimplePlayer2Bullets(Graphics2D g) {
        for (int i = 0; i < this.enemyBullets.size(); i++) {
            int x = (int) ((Bullet) this.enemyBullets.get(i)).position.x;
            int y = (int) ((Bullet) this.enemyBullets.get(i)).position.y;
            boolean isAlive = ((Bullet) this.enemyBullets.get(i)).isAlive;
            if ((x < this.mapPosition) || (x > this.backgroundSizeOfWidth / 3.0F + this.mapPosition)
                    || (y < 0) || (y > this.backgroundSizeOfHeight / 3.0F) || (!isAlive)) {
                this.enemyBullets.remove(i);
            } else
                ((Bullet) this.enemyBullets.get(i)).drawBubblet(g, this.mapPosition);
        }

    }

    private void playPreBossBattle(Graphics2D big2) {
    }

    public void paint(Graphics g)//屏幕右上角操作流程
    {
        super.paint(g);
        g.setColor(Color.GRAY);
        g.fillRect(0, 0, this.backgroundSizeOfWidth, this.backgroundSizeOfHeight);
        g.setColor(Color.BLACK);
        g.setFont(new Font(null, 1, 30));
        g.drawString("操作控制", 740, 100);
        g.setFont(new Font(null, 1, 20));
        g.drawString("W 上, S 下", 740, 150);
        g.drawString("A 左, D 右", 745, 180);
        g.drawString("J 开枪", 745, 210);
        g.drawString("K 跳跃", 745, 240);
        g.drawString("切枪:", 745, 270);
        g.drawString("1、2、3、4", 745, 300);
        g.drawString("Esc 退出", 745, 330);
        g.setFont(new Font(null, 1, 16));

    }

    public void update(Graphics g) {
        Graphics2D g2 = (Graphics2D) g;
        if (this.firstTime) {
            Dimension dim = getSize();//外形尺寸
            int w = dim.width;//宽
            int h = dim.height;//高
            this.bi = ((BufferedImage) createImage(w, h));//图像缓冲区创建图片
            this.big = this.bi.createGraphics();//2D的图形界面方法
        }
        this.big.drawImage(Imgs.BACKGROUND01, (int) this.scenePosition.x, (int) this.scenePosition.y,//场景位置的x,y
                this.backgroundSizeOfWidth, (int) this.scenePosition.y + 672,
                (int) this.mapPosition, 0, (int) this.mapPosition + this.backgroundSizeOfWidth / 3,//地图背景
                224, null);
        drawNPC(this.big);
        if (this.showBossScene) {//boss场景
            playPreBossBattleAnimation(this.big);//boss战斗动画
        }
        if (this.bossBattle) {//boss战斗
            playBossBattleAnimation(this.big);
            ;//boss战斗动画jfjjjjjkkdakj
        }
        for (int i = 0; i < this.enemys.size(); i++) {
            ((SimplePlayer2) this.enemys.get(i)).drawPlayer(this.big, this.mapPosition);
        }//遍历地图上画敌人
        this.hero.drawPlayer(this.big, this.mapPosition);//画地图上的英雄
        drawHeroBullets(this.big);//画英雄子弹
        drawSimplePlayer2Bullets(this.big);//画敌人子弹
        g2.setColor(Color.GRAY);//设置窗口背景颜色为灰色
        g2.drawImage(this.bi, (int) this.bgPosition.x, (int) this.bgPosition.y,//背景x,y位置
                (int) (this.backgroundSizeOfWidth / 1.3D), (int) (this.backgroundSizeOfHeight / 1.3D), this);//背景宽和高
    }

    public void run() {
//        try {
//            Thread.sleep(2000L);
//        } catch (InterruptedException e) {
//            e.printStackTrace();
//        }
        setVisible(true);
        while (true) {
            repaint(0, 0, 720, this.backgroundSizeOfHeight);
            try {
                Thread.sleep(1000 / FPS);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            heroMove();//英雄移动
            checkHeroIsDropping();//英雄掉落
            heroDeathCheck();//检查英雄死亡
            setEnemyVisibility();//设置敌人可见性
            enemysMove();//敌人移动
            enemysDeathCheck();//检查敌人死亡
            if ((this.hero.position.x >= 880.0F) && (this.hero.position.x <= 950.0F) &&
                    (this.hero.position.y >= 20.0F) && (this.hero.position.y <= 111.0F))//英雄的位置
                this.passedFirstMovingGrassLand = true;//第一次经过草地的移动
            if ((this.hero.position.x >= 1170.0F) && (this.hero.position.y >= 20.0F) &&
                    (this.hero.position.y <= 111.0F)) //英雄的位置
            {
                this.passedSecondMovingGrassLand = true;//第二次经过草地的移动
            }
            if ((this.hero.state == 5) && (this.heroNum > 0)) {//英雄的状态和英雄的
                heroBirth();//英雄生成
            }

            if ((this.hero.position.x > 3260.0F) && //英雄位置
                    (this.mapPosition < 3328.0F)) {
                this.showBossScene = true;//生成boss场景

                this.preBossBgSoundThread = new PreBossBackgroundSoundThread();//boss背景声音

            }

        }
    }

    private void heroMove() {            //英雄移动
        if ((this.hero.state != 4) && (this.hero.state != 5))
            if ((!this.hero.jumping) && (!this.jumpDown) && (!this.leftDown) && (!this.rightDown) && (!this.upDown) && (!this.downDown)) {
                this.hero.state = 0;
                if (this.hero.towardsLeft) {
                    this.hero.direction = 6;
                } else if (this.hero.towardsRight) {
                    this.hero.direction = 2;
                }

            } else if (this.hero.jumping) {
                if (this.upDown) {
                    this.hero.direction = 0;
                    this.hero.state = 2;
                    if ((this.leftDown) && (!this.showBossScene)) {
                        this.hero.direction = 7;
                        this.hero.towardsLeft = true;
                        this.hero.towardsRight = false;
                        if (this.hero.position.x > 8.0F + this.mapPosition)
                            this.hero.position.x -= this.walkSpeed;
                    } else if ((this.rightDown) && (!this.showBossScene)) {
                        this.hero.direction = 1;
                        this.hero.towardsLeft = false;
                        this.hero.towardsRight = true;
                        moveForwardToRight();
                    }
                } else if (this.downDown) {
                    this.hero.direction = 4;
                    this.hero.state = 2;
                    if (this.leftDown) {
                        this.hero.direction = 5;
                        this.hero.towardsLeft = true;
                        this.hero.towardsRight = false;
                        if (this.hero.position.x > 8.0F + this.mapPosition)
                            this.hero.position.x -= this.walkSpeed;
                    } else if ((this.rightDown) && (!this.showBossScene)) {
                        this.hero.direction = 3;
                        this.hero.towardsLeft = false;
                        this.hero.towardsRight = true;
                        moveForwardToRight();
                    }
                } else if ((this.leftDown) && (!this.showBossScene)) {
                    this.hero.direction = 6;
                    this.hero.state = 2;
                    this.hero.towardsLeft = true;
                    this.hero.towardsRight = false;
                    if (this.hero.position.x > 8.0F + this.mapPosition)
                        this.hero.position.x -= this.walkSpeed;
                } else if ((this.rightDown) && (!this.showBossScene)) {
                    this.hero.direction = 2;
                    this.hero.state = 2;
                    this.hero.towardsLeft = false;
                    this.hero.towardsRight = true;
                    moveForwardToRight();
                }

            } else if ((this.leftDown) && (!this.showBossScene)) {
                if ((this.jumpDown) && (isGrassLand(this.hero.position.x, this.hero.position.y + 1.0F))) {
                    this.hero.direction = 6;
                    this.hero.state = 2;
                    this.hero.towardsLeft = true;
                    this.hero.towardsRight = false;
                    this.hero.jumping = true;
                    this.hero.jumpFinished = false;
                } else if (this.upDown) {
                    this.hero.state = 1;
                    this.hero.direction = 7;
                    this.hero.towardsLeft = true;
                    this.hero.towardsRight = false;
                    if (this.hero.position.x > 8.0F + this.mapPosition)
                        this.hero.position.x -= this.walkSpeed;
                } else if (this.downDown) {
                    this.hero.direction = 5;
                    this.hero.state = 1;
                    this.hero.towardsLeft = true;
                    this.hero.towardsRight = false;
                    if (this.hero.position.x > 8.0F + this.mapPosition)
                        this.hero.position.x -= this.walkSpeed;
                } else {
                    this.hero.state = 1;
                    this.hero.direction = 6;
                    this.hero.towardsLeft = true;
                    this.hero.towardsRight = false;
                    if (this.hero.position.x > 8.0F + this.mapPosition)
                        this.hero.position.x -= this.walkSpeed;
                }
            } else if ((this.rightDown) && (!this.showBossScene)) {
                if ((this.jumpDown) && (isGrassLand(this.hero.position.x, this.hero.position.y + 1.0F))) {
                    this.hero.direction = 2;
                    this.hero.state = 2;
                    this.hero.towardsLeft = false;
                    this.hero.towardsRight = true;
                    this.hero.jumping = true;
                    this.hero.jumpFinished = false;
                } else if (this.upDown) {
                    this.hero.direction = 1;
                    this.hero.state = 1;
                    this.hero.towardsLeft = false;
                    this.hero.towardsRight = true;
                    moveForwardToRight();
                } else if (this.downDown) {
                    this.hero.direction = 3;
                    this.hero.state = 1;
                    this.hero.towardsLeft = false;
                    this.hero.towardsRight = true;
                    moveForwardToRight();
                } else {
                    this.hero.direction = 2;
                    this.hero.state = 1;
                    this.hero.towardsLeft = false;
                    this.hero.towardsRight = true;
                    moveForwardToRight();
                }
            } else if (this.upDown) {
                if ((this.jumpDown) && (isGrassLand(this.hero.position.x, this.hero.position.y + 1.0F))) {
                    this.hero.direction = 0;
                    this.hero.state = 2;
                    this.hero.jumpFinished = false;
                    this.hero.jumping = true;
                } else {
                    this.hero.state = 0;
                    this.hero.direction = 0;
                }
            } else if (this.downDown) {
                if (this.jumpDown) {
                    this.hero.state = 1;
                } else {
                    this.hero.state = 3;
                    if (this.hero.direction == 3) {
                        this.hero.direction = 2;
                    } else if (this.hero.direction == 5) {
                        this.hero.direction = 6;
                    }
                }
            } else if ((this.jumpDown) && (isGrassLand(this.hero.position.x, this.hero.position.y + 1.0F))) {
                this.hero.state = 2;
                this.hero.jumping = true;
                this.hero.jumpFinished = false;
            }
    }

    private void checkHeroIsDropping() {         //检测英雄掉落方法
        if ((this.hero.state != 4) && (this.hero.state != 5))
            if (!isGrassLand(this.hero.position.x, this.hero.position.y + 1.0F)) {
                if ((this.hero.position.y < 206.0F) && (!this.hero.jumping)) {
                    this.hero.state = 6;
                    this.hero.position.y += 2.0F;
                } else if ((this.hero.position.y > 206.0F) && (this.hero.jumping)) {
                    this.hero.position.y = 206.0F;
                    this.hero.state = 0;
                }

                if ((this.hero.position.y >= 206.0F) && (!isGrassLand(this.hero.position.x, this.hero.position.y + 1.0F))) {
                    if (this.hero.visible) {
                        this.hero.state = 4;
//                        this.hero.deathEventType = 1;
//                        SoundUtils.playSound("audios/death.wav");
                    }
                }
            } else if ((this.hero.jumping) && (this.hero.jumpFinished)) {
                this.hero.jumping = false;
                if ((this.hero.direction == 0) || (this.hero.direction == 1) || (this.hero.direction == 2) || (this.hero.direction == 3)) {
                    this.hero.direction = 2;
                } else if ((this.hero.direction == 4) || (this.hero.direction == 5) || (this.hero.direction == 6) || (this.hero.direction == 7)) {
                    this.hero.direction = 6;
                }
                this.hero.state = 0;
//                SoundUtils.playSound("audios/jumpping.wav");
            }
    }
//英雄碰撞死亡
    private void heroDeathCheck()
    {
        if ((this.hero.state != 4) && (this.hero.state != 5)) {
            for (int i = 0; i < this.enemyBullets.size(); i++) {
                Bullet b = (Bullet)this.enemyBullets.get(i);
                if ((b.position.x > this.hero.position.x - this.hero.width / 2 / 3) && (b.position.x < this.hero.position.x + this.hero.width / 2 / 3) &&
                        (b.position.y > this.hero.position.y - this.hero.height / 3) && (b.position.y < this.hero.position.y) &&
                        (this.hero.visible)) {
                    this.hero.state = 4;
                    this.hero.deathEventType = 0;
                    this.enemyBullets.remove(i);
                }
            }

            for (int i = 0; i < this.enemys.size(); i++) {
                SimplePlayer2 e = (SimplePlayer2)this.enemys.get(i);
                if ((e.state != 4) && (e.state != 5) &&
                        (this.hero.position.x > e.position.x - this.hero.width / 2 / 3 - 11.0F) && (this.hero.position.x < e.position.x + 11.0F + this.hero.width / 2 / 3) &&
                        (this.hero.position.y > e.position.y - 26.0F) && (this.hero.position.y < e.position.y + this.hero.height / 3)) {
                    if (this.hero.visible) {
                        this.hero.state = 4;
                        this.hero.deathEventType = 2;
                    }
                    else {
                        e.state = 4;
                    }
                }
            }
        }
    }

    private void setEnemyVisibility() {
    }

    private void enemysMove() {
    }

    //敌人碰撞死亡
    private void enemysDeathCheck() {
        for (int i = 0; i < this.enemys.size(); i++) {
            if ((((SimplePlayer2)this.enemys.get(i)).state == 5) ||
                    (((SimplePlayer2)this.enemys.get(i)).position.x > this.mapPosition + this.backgroundSizeOfWidth / 3)) {
                if (((SimplePlayer2)this.enemys.get(i)).visible)
                    this.enemys.remove(i);
            }
            else if (((SimplePlayer2)this.enemys.get(i)).position.x < this.mapPosition) {
                this.enemys.remove(i);
            }
        }
        for (int i = 0; i < this.enemys.size(); i++) {
            SimplePlayer2 e = (SimplePlayer2)this.enemys.get(i);
            for (int j = 0; j < this.heroBullets.size(); j++) {
                Bullet b = (Bullet)this.heroBullets.get(j);
                if (e.touchHeroBulletCheck(b)) {
                    if ((e.state == 4) || (e.state == 5)) break;
                    e.beShotted(b.energy);
                    this.heroBullets.remove(j);

                    break;
                }
            }
        }
//        if ((this.boss != null) && (this.boss.state != 4) && (this.boss.state != 5)) {
//            for (int i = 0; i < this.heroBullets.size(); i++) {
//                Bullet b = (Bullet)this.heroBullets.get(i);
//                if (this.boss.touchHeroBulletCheck(b)) {
//                    this.boss.beShotted(b.energy);
//                    this.heroBullets.remove(i);
//                }
//                if ((this.boss.state == 4) && (this.stopBossBgSound)) {
//                    this.enemyBullets.removeAll(this.enemyBullets);
//                    this.enemys.removeAll(this.enemys);
//                    AudioPlayer.player.stop(this.bossBgSound.as);
//                    this.bossBgSound.playSound = false;
//                    this.stopBossBgSound = false;
//                }
//            }
//        }

    }

    private FloatPoint findBirthPlace() {       //场景移动
        if ((this.passedFirstMovingGrassLand) && (this.grassLands[(this.grassLands.length - 2)].position.x * 32.0F + 32.0F > this.mapPosition)) {
            for (int j = (int) (this.grassLands[(this.grassLands.length - 2)].position.x * 32.0F + 50.0F); j < this.backgroundSizeOfWidth / 3 + this.mapPosition; j += 32) {
                for (int i = 0; i < this.backgroundSizeOfHeight / 3; i += 3) {
                    if ((isGrassLand(j, i)) && (isGrassLand(j + 10, i))) {
                        if (j % 32 < 16) {
                            j += 10;
                        }
                        return new FloatPoint(j, 0.0F);
                    }
                }
            }
        } else if ((this.passedSecondMovingGrassLand) && (this.grassLands[(this.grassLands.length - 1)].position.x * 32.0F + 32.0F > this.mapPosition)) {
            for (int j = (int) (this.grassLands[(this.grassLands.length - 1)].position.x * 32.0F + 50.0F); j < this.backgroundSizeOfWidth / 3 + this.mapPosition; j += 32) {
                for (int i = 0; i < this.backgroundSizeOfHeight / 3; i += 3) {
                    if ((isGrassLand(j, i)) && (isGrassLand(j + 10, i))) {
                        if (j % 32 < 16) {
                            j += 10;
                        }
                        return new FloatPoint(j, 0.0F);
                    }
                }
            }
        } else {
            for (int j = (int) this.mapPosition + 20; j < this.backgroundSizeOfWidth / 3 + this.mapPosition; j += 32) {
                for (int i = 0; i < this.backgroundSizeOfHeight / 3; i += 3) {
                    if ((isGrassLand(j, i)) && (isGrassLand(j + 10, i)))
                        return new FloatPoint(j + 10, 0.0F);
                }
            }
        }
        return null;
    }

    private int finBirthDirection() {
        return 2;
    }

    //判断是否安全跳跃
    private boolean isASafetyJump(FloatPoint position, int direction) {
        if (direction == 2) {
            if ((!this.grassLands[(this.grassLands.length - 2)].beTouched) &&
                    (position.x < this.grassLands[(this.grassLands.length - 2)].position.x * 32.0F)) {
                if (this.grassLands[(this.grassLands.length - 2)].movingDirection == 3) {
                    float delta = this.grassLands[(this.grassLands.length - 2)].position.x * 32.0F - 0.8F;
                    if (delta < 23.399999999999999D) {
                        delta = 23.4F - delta + 23.4F;
                    }
                    if ((position.x + 40.0F > delta) && (position.x + 40.0F < delta + 32.0F)) {
                        return true;
                    }
                }
                if (this.grassLands[(this.grassLands.length - 2)].movingDirection == 1) {
                    float delta = this.grassLands[(this.grassLands.length - 2)].position.x * 32.0F + 0.8F;
                    if (delta > 25.799999F) {
                        delta = 25.799999F - (delta - 25.799999F);
                    }
                    if ((position.x + 40.0F > delta) && (position.x + 40.0F < delta + 32.0F)) {
                        return true;
                    }
                }
            }

            if ((!this.grassLands[(this.grassLands.length - 1)].beTouched) &&
                    (position.x < this.grassLands[(this.grassLands.length - 1)].position.x * 32.0F)) {
                if (this.grassLands[(this.grassLands.length - 1)].movingDirection == 3) {
                    float delta = this.grassLands[(this.grassLands.length - 2)].position.x * 32.0F - 0.8F;
                    if (delta < 32.399999999999999D) {
                        delta = 32.400002F - delta + 32.400002F;
                    }
                    if ((position.x + 40.0F > delta) && (position.x + 40.0F < delta + 32.0F)) {
                        return true;
                    }
                }
                if (this.grassLands[(this.grassLands.length - 1)].movingDirection == 1) {
                    float delta = this.grassLands[(this.grassLands.length - 1)].position.x * 32.0F + 0.8F;
                    if (delta > 34.799999F) {
                        delta = 34.799999F - (delta - 34.799999F);
                    }
                    if ((position.x + 40.0F > delta) && (position.x + 40.0F < delta + 32.0F)) {
                        return true;
                    }
                }
            }
            float x = position.x + 20.0F;
            for (float y = position.y - 40.0F;
                 (y < 206.0F) && (x < this.mapPosition + this.backgroundSizeOfWidth / 3 - 14.0F); y += 2.0F) {
                if (isGrassLand(x, y + 2.0F))
                    return true;
                x += 1.0F;
            }

            return false;
        }
        if (direction == 6) {
            if ((!this.grassLands[(this.grassLands.length - 2)].beTouched) &&
                    (position.x > this.grassLands[(this.grassLands.length - 2)].position.x * 32.0F + 32.0F)) {
                if (this.grassLands[(this.grassLands.length - 2)].movingDirection == 3) {
                    float delta = this.grassLands[(this.grassLands.length - 2)].position.x * 32.0F - 0.8F;
                    if (delta < 23.399999999999999D) {
                        delta = 23.4F - delta + 23.4F;
                    }
                    if ((position.x - 40.0F > delta) && (position.x - 40.0F < delta + 32.0F)) {
                        return true;
                    }
                }
                if (this.grassLands[(this.grassLands.length - 2)].movingDirection == 1) {
                    float delta = this.grassLands[(this.grassLands.length - 2)].position.x * 32.0F + 0.8F;
                    if (delta > 25.799999F) {
                        delta = 25.799999F - (delta - 25.799999F);
                    }
                    if ((position.x - 40.0F > delta) && (position.x - 40.0F < delta + 32.0F)) {
                        return true;
                    }
                }
            }

            if ((!this.grassLands[(this.grassLands.length - 1)].beTouched) &&
                    (position.x > this.grassLands[(this.grassLands.length - 1)].position.x * 32.0F + 32.0F)) {
                if (this.grassLands[(this.grassLands.length - 1)].movingDirection == 3) {
                    float delta = this.grassLands[(this.grassLands.length - 2)].position.x * 32.0F - 0.8F;
                    if (delta < 32.399999999999999D) {
                        delta = 32.400002F - delta + 32.400002F;
                    }
                    if ((position.x - 40.0F > delta) && (position.x - 40.0F < delta + 32.0F)) {
                        return true;
                    }
                }
                if (this.grassLands[(this.grassLands.length - 1)].movingDirection == 1) {
                    float delta = this.grassLands[(this.grassLands.length - 1)].position.x * 32.0F + 0.8F;
                    if (delta > 34.799999F) {
                        delta = 34.799999F - (delta - 34.799999F);
                    }
                    if ((position.x - 40.0F > delta) && (position.x - 40.0F < delta + 32.0F)) {
                        return true;
                    }
                }
            }

            float x = position.x - 20.0F;
            for (float y = position.y - 40.0F;
                 (y < 206.0F) && (x > 14.0F); y += 2.0F) {
                if (isGrassLand(x, y + 2.0F))
                    return true;
                x -= 1.0F;
            }

            return false;
        }

        return false;
    }

    private boolean isGrassLand(float x, float y) {      //判断是否站在草地上
        if (this.hero.towardsRight) {
            for (int i = 0; i < this.grassLands.length; i++) {
                if ((x > this.grassLands[i].position.x * 32.0F - 7.0F) && (x < (this.grassLands[i].position.x + this.grassLands[i].length) * 32.0F + 3.0F) &&
                        (y > this.grassLands[i].position.y) && (y < this.grassLands[i].position.y + 5.0F))
                    return true;
            }
        } else if (this.hero.towardsLeft) {
            for (int i = 0; i < this.grassLands.length; i++) {
                if ((x > this.grassLands[i].position.x * 32.0F - 7.0F) && (x < (this.grassLands[i].position.x + this.grassLands[i].length) * 32.0F + 1.0F) &&
                        (y > this.grassLands[i].position.y) && (y < this.grassLands[i].position.y + 5.0F))
                    return true;
            }
        } else if ((!this.hero.towardsLeft) && (!this.hero.towardsRight)) {
            for (int i = 0; i < this.grassLands.length; i++) {
                if ((x > this.grassLands[i].position.x * 32.0F) && (x < (this.grassLands[i].position.x + this.grassLands[i].length) * 32.0F) &&
                        (y > this.grassLands[i].position.y) && (y < this.grassLands[i].position.y + 5.0F))
                    return true;
            }
        }
        return false;
    }

    private void moveForwardToRight() {
        if ((this.hero.state != 4) && (this.hero.state != 5))
            if ((this.hero.position.x - this.mapPosition < this.backgroundSizeOfWidth / 6.0F) || (this.mapPosition >= this.sceneWidth - this.backgroundSizeOfWidth / 3)) {
                if (this.hero.position.x - this.mapPosition < (this.backgroundSizeOfWidth - 20.0F) / 3.0F)
                    this.hero.position.x += this.walkSpeed;
            } else {
                if (this.mapPosition < this.sceneWidth - this.backgroundSizeOfWidth / 3) {
                    this.mapPosition += this.walkSpeed;
                } else {
                    this.mapPosition = (this.sceneWidth - this.backgroundSizeOfWidth / 3.0F);
                }
                this.hero.position.x = (this.backgroundSizeOfWidth / 6.0F + this.mapPosition);
            }
    }

    private void heroBirth() {     //英雄的生成
        if ((this.mapPosition > 680.0F) && (!this.passedFirstMovingGrassLand)) {
            GrassLand movingGrassLand = new GrassLand(new FloatPoint(24.0F, 110.0F), 1);
            movingGrassLand.movingDirection = 3;
            this.grassLands[(this.grassLands.length - 2)] = movingGrassLand;
            this.mapPosition = 680.0F;
            this.hero = new Hero(new FloatPoint(700.0F, 0.0F), 2);
        } else if ((this.hero.position.x > 740.0F) && (!this.passedFirstMovingGrassLand)) {
            GrassLand movingGrassLand = new GrassLand(new FloatPoint(24.0F, 110.0F), 1);
            movingGrassLand.movingDirection = 3;
            this.grassLands[(this.grassLands.length - 2)] = movingGrassLand;
            this.hero = new Hero(new FloatPoint(this.mapPosition + 20.0F, 0.0F), 2);
        } else if ((this.mapPosition > 970.0F) && (!this.passedSecondMovingGrassLand)) {
            GrassLand movingGrassLand = new GrassLand(new FloatPoint(33.0F, 110.0F), 1);
            movingGrassLand.movingDirection = 3;
            this.grassLands[(this.grassLands.length - 1)] = movingGrassLand;
            this.mapPosition = 970.0F;
            this.hero = new Hero(new FloatPoint(990.0F, 0.0F), 2);
        } else if ((this.hero.position.x > 1030.0F) && (!this.passedSecondMovingGrassLand)) {
            GrassLand movingGrassLand = new GrassLand(new FloatPoint(33.0F, 110.0F), 1);
            movingGrassLand.movingDirection = 3;
            this.grassLands[(this.grassLands.length - 1)] = movingGrassLand;
            this.hero = new Hero(new FloatPoint(this.mapPosition + 20.0F, 0.0F), 2);
        } else {
            this.hero = new Hero(findBirthPlace(), finBirthDirection());
        }
        this.hero.state = 0;
        this.heroNum -= 1;
        this.leftDown = false;
        this.rightDown = false;
        this.upDown = false;
        this.downDown = false;
        this.jumpDown = false;

    }

    public static void main(String[] args) {
        new Thread(new World()).start();//frame窗口用线程启动
    }
}

